package bview

import (
	"fmt"
	//"html/template"

	"bmodel"
	_ "io/ioutil"
	_ "net/http"
	"strconv"
	_ "time"

	"github.com/gin-gonic/gin"
)

/*
 * index GET
 * para none
 * return 200, html
 */
func IndexGET(c *gin.Context) {
	blockinfos, erro := bmodel.GetPublishedSublevelBlockIdsAndNames()
	if erro != nil {
		c.String(200, fmt.Sprintf("%s", erro))
	} else {
		//注意此处和后台共用 DEFAULT_ARTICLE_LIST_PAGE_STRING
		articlePage := c.DefaultQuery("page", DEFAULT_ARTICLE_LIST_PAGE_STRING)
		articlePerpage := c.DefaultQuery("perpage", DEFAULT_NUMBER_OF_ARTICLE_IN_ONE_PAGE)
		var articlePageInt, articlePerpageInt int
		var errppi, errpppi error
		articlePageInt, errppi = strconv.Atoi(articlePage)
		articlePerpageInt, errpppi = strconv.Atoi(articlePerpage)

		if errppi != nil || errpppi != nil {
			c.String(200, ERROR_GET_PARA_ERROR)
			return
		}

		if articleContents, merr := bmodel.GetarticlesByPageAndState(articlePageInt, articlePerpageInt); merr != nil {
			c.String(200, fmt.Sprintf("%s", merr))
		} else {
			var retMap = make(map[string]interface{})

			articleNums, errb := bmodel.Countarticles(true)
			if errb != nil {
				c.String(200, fmt.Sprintf("%s", errb))
				return
			}

			pageIndexs := bmodel.GeneratePages(articleNums, DEFAULT_NUMBER_OF_PAGES,
				articlePageInt, articlePerpageInt)
			retMap["pageIndexs"] = pageIndexs
			retMap["prepage"] = articlePageInt
			retMap["defset"] = bmodel.GetarticleDefaultSettings()
			retMap["content"] = articleContents
			retMap["blockinfos"] = blockinfos
			c.HTML(200, "blog/index.html", retMap)
		}
	}
}

/*
 * index GET
 * para none
 * return 200, html
 */
func ArticlelistGET(c *gin.Context) {
	blockinfos, erro := bmodel.GetPublishedSublevelBlockIdsAndNames()
	if erro != nil {
		c.String(200, fmt.Sprintf("%s", erro))
	} else {
		//注意此处和后台共用 DEFAULT_ARTICLE_LIST_PAGE_STRING
		blockId := c.DefaultQuery("blockid", DEFAULT_ARTICLE_LIST_BLOCKID_STRING)
		articlePage := c.DefaultQuery("page", DEFAULT_ARTICLE_LIST_PAGE_STRING)
		articlePerpage := c.DefaultQuery("perpage", DEFAULT_NUMBER_OF_ARTICLE_IN_ONE_PAGE)
		var blockIdInt, articlePageInt, articlePerpageInt int
		var errbii, errppi, errpppi error
		blockIdInt, errbii = strconv.Atoi(blockId)
		articlePageInt, errppi = strconv.Atoi(articlePage)
		articlePerpageInt, errpppi = strconv.Atoi(articlePerpage)

		if errbii != nil || errppi != nil || errpppi != nil {
			c.String(200, ERROR_GET_PARA_ERROR)
			return
		}

		if articleContents, merr := bmodel.GetarticlesByBlockidAndPageAndState(blockIdInt, articlePageInt, articlePerpageInt); merr != nil {
			c.String(200, fmt.Sprintf("%s", merr))
		} else {
			var retMap = make(map[string]interface{})

			/*
				articleNums, errb := bmodel.Countarticles(true)
				if errb != nil {
					c.String(200, fmt.Sprintf("%s", errb))
					return
				}
			*/
			articleNums, errb := bmodel.CountarticlesByBlockId(true, blockIdInt)
			if errb != nil {
				c.String(200, fmt.Sprintf("%s", errb))
				return
			}
			pageIndexs := bmodel.GeneratePages(articleNums, DEFAULT_NUMBER_OF_PAGES,
				articlePageInt, articlePerpageInt)
			retMap["pageIndexs"] = pageIndexs
			retMap["prepage"] = articlePageInt

			blockinfo, berr := bmodel.GetblockById(blockIdInt)
			if berr == nil {
				retMap["preblockid"] = blockId
				retMap["preblocktitle"] = blockinfo["Title"]
				retMap["defset"] = bmodel.GetarticleDefaultSettings()
				retMap["content"] = articleContents
				retMap["blockinfos"] = blockinfos
				c.HTML(200, "blog/articlelist.html", retMap)
			} else {
				c.String(200, fmt.Sprintf("%s", berr))
			}
		}
	}
}

/*
 * articledetail get
 * para c *gin.Context(GER para: id)
 * return 200, html/String
 */
func ArticledetailGET(c *gin.Context) {
	blockinfos, erro := bmodel.GetPublishedSublevelBlockIdsAndNames()
	if erro != nil {
		c.String(200, fmt.Sprintf("%s", erro))
	} else {
		articleId := c.DefaultQuery("id", DEFAULT_ARTICLE_ID)
		var articleIdInt int
		var errpii error
		articleIdInt, errpii = strconv.Atoi(articleId)

		if errpii != nil {
			c.String(200, ERROR_GET_PARA_ERROR)
			return
		}

		if articleContent, merr := bmodel.GetarticleById(articleIdInt); merr != nil {
			c.String(200, fmt.Sprintf("%s", merr))
		} else {
			//note: describe is a html
			var retMap = make(map[string]interface{})
			retMap["defset"] = bmodel.GetarticleDefaultSettings()
			retMap["content"] = articleContent
			retMap["blockinfos"] = blockinfos
			c.HTML(200, "blog/articledetail.html", retMap)
		}
	}
}
